import java.awt.*;

/* Ori Perl
 * 10/19/10
 * Software Design pd. 8
 * Holds information for where the desk is and what is on top of it it.
 * */
public class Desk {
	Point topR;
	Point lowL;
	int error = 2;
	int time;
	Calculator calc;
	public Desk(){
		topR = new Point(0,0);
		lowL= new Point(0,0);
		calc = null;
	}//end default constructor
	public Desk(Point tR,Point lL){
		topR = tR;
		lowL= lL;
		calc = null;
	}//end normal constructor
	public void addCalc(int c){
		calc = new Calculator(lowL,c);
		time =0;
	}//end add
	public void removeCalc (){
		time = 0;
		calc = null;
	}//end remove
	//checks if there is something on the desk based on the things location on the desk
	public boolean hasCalc(Point c){
		//check if the calculator exists and its on the desk
		if (calc != null && lowL.getY()>=calc.getTopRight().getY()){
			//check whether the click is inside the calc area with an error of x pixels
			if(!calc.isHit() && c.getY()<=lowL.getY()+error && c.getY()<=calc.getBottomLeft().getY()+error  
					&& c.getY()>= calc.getTopRight().getY()-error && 
					c.getX()>=calc.getBottomLeft().getX()-error && c.getX()<= calc.getTopRight().getX()+error){
				//resets so explosion can be viewed
				time = 0;
				calc.hit();
				return true;
			}//end if
		}//end if
		return false;
	}//end has
	public void moveCalc(){
		if (calc!=null){
			time +=1;
			//for the calculator
			if (!calc.isHit()){
				int move = 2;
				//max time * move = height of desk
				if (time*move >(lowL.getY()-topR.getY())){
					move = -move;
				}//end if
				//pauses when it reaches the top
				if (move <0 && time - (lowL.getY()-topR.getY()) < 30){
					move = 0;
				}
					calc.move(move);
				if (move <0 && calc.getTopRight().getY() >lowL.getY()){
					//System.out.println("deleted");
					removeCalc();
				}//end if
			}//end if
			//for the destroyed calculator
			else{
				if(time >150){
					removeCalc();
					//System.out.println("boom, deleted");
				}//end if
			}//end else
			//check if the calculator disappeared
		}//end if
	}//end move Calc
	public Calculator getCalc(){
		if (calc == null){
			return null;
		}//end if
		return calc;
	}//end get Calculator
}//end class
